package com.capgemini.remind.service;

import com.capgemini.commons.IdUtils;
import com.capgemini.core.PageList;
import com.capgemini.remind.mapper.CustomerGroupMapper;
import com.capgemini.remind.pojo.AnnouncementCustomerInfo;
import com.capgemini.remind.pojo.CustomerGroup;
import com.capgemini.remind.pojo.CustomerPromotion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * 
 * @author
 * @date
 */
@Service
@Transactional
public class CustomerGroupService {
	public final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private CustomerGroupMapper customerGroupMapper;


	public List<CustomerGroup> queryList(CustomerGroup customerGroup) {
		return customerGroupMapper.queryList(customerGroup);
	}

	public void queryPage(final PageList<CustomerGroup> pageList) {
		pageList.setItems(customerGroupMapper.queryPage(pageList));
	}


	public void queryCustomerPage(final PageList<AnnouncementCustomerInfo> pageList) {
		pageList.setItems(customerGroupMapper.queryCustomerPage(pageList));
	}

	public void queryCustomerPageForAdd(final PageList<AnnouncementCustomerInfo> pageList) {
		pageList.setItems(customerGroupMapper.queryCustomerPageForAdd(pageList));
	}

	public int deletePhysicalById(String id) {
		return customerGroupMapper.deletePhysicalById(id);
	}

	public int insert(CustomerGroup pojo) {
		Date now = new Date();

		pojo.setId(Long.valueOf(IdUtils.getNewId()));
		pojo.setIsCustom(true);
		pojo.setCreateBy("0");
		pojo.setLastUpdateBy("0");
		pojo.setCreateDate(now);
		pojo.setLastUpdateDate(now);


		return customerGroupMapper.insert(pojo);
	}

	public int updateById(CustomerGroup pojo) {
		pojo.setLastUpdateDate(new Date());
		pojo.setLastUpdateBy("0");
		return customerGroupMapper.updateById(pojo);
	}

	public CustomerGroup getById(String id) {
		return customerGroupMapper.getById(id);
	}

	public int delCustomerFromGroup(String dealerNum, String customerId, String groupId) {
		return customerGroupMapper.delCustomerFromGroup(dealerNum, customerId, groupId);
	}

	public void addCustomerToGroup(String dealerNum, String groupId, List<String> selectedIdList) {
		String batchId = IdUtils.getNewId();
		customerGroupMapper.addCustomerToGroup(batchId, dealerNum, groupId, new ArrayList<String>(new HashSet(selectedIdList)));
		customerGroupMapper.processCustomerToGroup(batchId);
	}
}
